<template>
    <kinesis-container>
    <div class="login-wrap">
      <kinesis-element :parallaxStrength="4" :type="parallax">
        <div class="login-box">
          <img src="./shifeng.png" class="nice-logo" />
          <p class="login">LOGIN</p>
          <div class="login-form">
            <el-form :model="user" :rules="formRules" ref="login-form">
              <div class="login-input">
                <el-form-item prop="username">
                  <el-input
                    class="login-text"
                    type="text"
                    placeholder="请输入手机号"
                    v-model="user.username"
                  ></el-input>
                  <span class="login-focus"></span>
                  <span class="login-symbol">
                    <i class="iconfont icon-phone"></i>
                  </span>
                </el-form-item>
              </div>
              <div class="login-input">
                <el-form-item prop="password">
                  <el-input
                    class="login-text"
                    type="password"
                    placeholder="请输入密码"
                    v-model="user.password"
                    @keyup.enter.native="login('ruleForm')"
                  ></el-input>
                  <span class="login-focus"></span>
                  <span class="login-symbol">
                    <i class="iconfont icon-mima"></i>
                  </span>
                </el-form-item>
              </div>
              <div class="login-footer">
                <div class="login-btn-wrap">
                  <el-button
                    class="login-btn"
                    type="primary"
                    :loading="loginLoading"
                    @click="onLogin"
                    >登录</el-button
                  >
                </div>
              </div>
            </el-form>
          </div>
        </div>
      </kinesis-element>
    </div>
  </kinesis-container>
</template>

<script>
import { login } from '@/api/user'
import { KinesisContainer, KinesisElement } from 'vue-kinesis'
export default {
  name: 'LoginIndex',
  components: {
    KinesisContainer,
    KinesisElement
  },
  props: {},
  data () {
    return {
      user: {
        username: '13035751438',
        password: 'sleep_zjx',
        agree: true
      },
      loginLoading: false,
      parallax: 'depth',
      formRules: {
        username: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '请输入正确的号码格式', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请同意用户协议'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    onLogin () {
      this.$refs['login-form'].validate(valid => {
        if (!valid) {
          return
        }
        this.login()
      })
    },
    login () {
      this.loginLoading = true
      login(this.user).then(res => {
        if (this.user.username == res.data.username && this.user.password == res.data.password) {
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          window.localStorage.setItem('user', JSON.stringify(res.data.data))
          this.loginLoading = false
          this.$router.push({
            name: 'home'
          })
        } else {
          // console.log('登录失败', err)
          this.$message.error('登录失败，手机号或密码错误')
          this.loginLoading = false
          this.$router.push({
            name: 'Error'
          })
        }
        // console.log(res)
        // this.$message({
        //   message: '登录成功',
        //   type: 'success'
        // })
        //
        // this.loginLoading = false
        // this.$router.push({
        //   name: 'HelloWorld'
        // })
      }).catch(err => {
        console.log('登录失败', err)
        this.$message.error('登录失败，手机号或密码错误')
        this.loginLoading = false
        this.$router.push({
          name: 'Error'
        })
      })
    }
  }
}
</script>
<style lang="stylus">
 .login-wrap {
  width: 100%;
  min-height: 100vh;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  padding: 15px;
  background: url("./login_bg.jpg") no-repeat;
  background-size: cover;
  .login-box {
    overflow: hidden;
    height: 483px;
    position: relative;
    width: 388px;
    max-width: 388px;
    margin: 4em auto;
    background: #FFF;
    background: url("./loginbg1.jpg");
    background-size: cover;
    border-radius: 8px;
    box-shadow: 1px 2px 15px rgba(0,0,0,.3);
    background-repeat: no-repeat;
    background-position: bottom;
    text-align: center;
    -webkit-backface-visibility: hidden;
    -webkit-transition-property: -webkit-transform;
    -moz-transition-property: -moz-transform;
    transition-property: transform;
    -webkit-transition-duration: 0.3s;
    -moz-transition-duration: 0.3s;
    transition-duration: 0.3s;
    z-index: 80;
    .switch {
      font-size: 13px;
      position: absolute;
      right: 0px;
      bottom: 5px;
      width: 100%;
      text-align: center;
      color: #0ef3da;
      cursor: pointer;
      b {
        color: #0ef3da;
        font-weight: 600;
      }
    }
    .nice-logo {
      width: 70px;
      margin: 40px 0 0px;
    }
    p {
      margin-bottom: 45px;
    }
    .login-form {
      width: 296px;
      margin: 0 auto;
      .login-title {
        font-size: 24px;
        color: #333333;
        line-height: 1.2;
        text-align: center;
        width: 100%;
        display: block;
        padding-bottom: 54px;
      }
      .login-input {
        position: relative;
        width: 75%;
        z-index: 1;
        margin-bottom: 10px;
        .login-text {
          background-color: #fff;
          font-size: 14px;
          line-height: 1.5;
          color: #666666;
          display: block;
          width: 100%;
          border: 1px solid #e3e7ed;
          height: 42px;
          border-radius: 3px;
          padding: 0 30px 0 45px;
          .el-input__inner {
            font-size: 14px;
            line-height: 1.5;
            color: #666666;
            display: block;
            width: 100%;
            border: 0;
            height: 40px;
            padding: 0;
            &:-webkit-autofill,
            &:-webkit-autofill:hover,
            &:-webkit-autofill:focus {
              box-shadow:0 0 0 60px #fff inset;
              color: #666666;
            }
          }
        }
        .login-focus {
          display: block;
          position: absolute;
          border-radius: 3px;
          bottom: 0;
          left: 0;
          z-index: -1;
          width: 100%;
          height: 100%;
          box-shadow: 0px 0px 0px 0px;
          color: rgba(230, 230, 230, 0.8);
        }
        .login-symbol {
          font-size: 15px;
          display: -webkit-box;
          display: -webkit-flex;
          display: -moz-box;
          display: -ms-flexbox;
          display: flex;
          align-items: center;
          position: absolute;
          border-radius: 3px;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 100%;
          padding-left: 15px;
          pointer-events: none;
          color: #666666;
          -webkit-transition: all 0.4s;
          -o-transition: all 0.4s;
          -moz-transition: all 0.4s;
          transition: all 0.4s;
        }
        .login-text {
          .el-input__inner:focus + .el-input__inner {
            -webkit-animation: anim-shadow 0.5s ease-in-out forwards;
            animation: anim-shadow 0.5s ease-in-out forwards;
          }
        }
      }
      .login-footer {
        width: 100%;
        display: -webkit-box;
        display: -webkit-flex;
        display: -moz-box;
        display: -ms-flexbox;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        padding-top: 10px;
        .login-btn-wrap {
          width: 100%;
          display: block;
          position: relative;
          z-index: 1;
          border-radius: 3px;
          overflow: hidden;
          margin: 0 auto;
          .login-btn-bg {
            position: absolute;
            z-index: -1;
            width: 300%;
            height: 100%;
            background: #a64bf4;
            background: -webkit-linear-gradient(right, #00dbde, #fc00ff, #00dbde, #fc00ff);
            top: 0;
            left: -100%;
            -webkit-transition: all 0.4s;
            -o-transition: all 0.4s;
            -moz-transition: all 0.4s;
            transition: all 0.4s;
          }
          .login-btn {
            font-size: 15px;
            line-height: 1.5;
            color: #fff;
            text-transform: uppercase;
            width: 100%;
            height: 42px;
            border-radius: 3px;
            border: 0;
            background: #5dd5c8;
            display: -webkit-box;
            display: -webkit-flex;
            display: -moz-box;
            display: -ms-flexbox;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 0 25px;
          }
          &:hover .login-btn-bg {
            left: 0;
          }
        }
      }
    }
  }
}

@-webkit-keyframes anim-shadow {
  to {
    box-shadow: 0px 0px 70px 25px;
    opacity: 0;
  }
}

@keyframes anim-shadow {
  to {
    box-shadow: 0px 0px 70px 25px;
    opacity: 0;
  }
}
.login {
  color: red;
  font-size: 30px;
  font-weight: bold;
  font-family:'Times New Roman', Times, serif
}
</style>

